package com.flydog.taskManager.common.converter;

import com.flydog.taskManager.dto.ProcessStatus;
import org.springframework.stereotype.Component;

import javax.persistence.Converter;
import javax.persistence.AttributeConverter;

@Converter
@Component
public class ProcessStatusConverter implements AttributeConverter<ProcessStatus, Integer> {

    @Override
    public Integer convertToDatabaseColumn(ProcessStatus processStatus) {
        return processStatus.getIndex();
    }

    @Override
    public ProcessStatus convertToEntityAttribute(Integer integer) {
        if (integer != null) {
            for (ProcessStatus status: ProcessStatus.values()) {
                if(status.getIndex() == integer) {
                    return status;
                }
            }
        }
        return null;
    }
}
